<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/7/18
 * Time: 13:04
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\admin\controller;

use app\common\lib\Show;
use app\common\lib\Status;
use think\App;
use think\Exception;
use think\facade\View;
use app\common\business\Category as BusObj;

class Category extends AdminBase
{
    protected ?BusObj $busObj = null;

    public function __construct(App $app)
    {
        $this->busObj = new BusObj();
        parent::__construct($app);
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            $limit = $this->request->param('limit', 1, 'intval');
            $map = [
                ['status', '<>', Status::Mysql_delete]
            ];
            try {
                $lists = $this->busObj->getIndexPage($map, $limit);
            } catch (\Exception $e) {
                return Show::json_show(Status::Error, $e->getMessage(), [], 0);
            }
            //dump($lists);exit();
            return Show::json_show(Status::Success, "ok", $lists['data'], $lists['total']);
        }
        return View::fetch('', [

        ]);
    }

    /**
     * @return string|\think\response\Json
     * @throws \think\Exception
     */
    public function add()
    {
        $pid = $this->request->param('pid', 0, 'trim,intval');
        //dump($pid);exit();
        if ($this->request->isPost()) {
            //获取所有字段
            $data = $this->request->param();
            try {
                $result = $this->busObj->add($data);
            } catch (\Exception $e) {
                return Show::json_show(Status::Error, $e->getMessage());
            }
            if ($result) {
                return Show::json_show(Status::Success, "操作成功");
            } else {
                return Show::json_show(Status::Error, "操作失败");
            }
        }
        $cate = $this->busObj->getCateTree();
        //dump($cate);exit();
        return View::fetch('', [
            'cate' => $cate,
            'pid' => $pid
        ]);
    }

    public function dialog()
    {
        $category = $this->busObj->getCategoryByPid(0, "id,title as name,pid");
        //halt($category);
        return View::fetch('', [
            "category" => Show::json_msg($category)
        ]);
    }
    public function getByPid(){
        $pid = $this->request->param('pid',0,'intval');
        $category = $this->busObj->getCategoryByPid($pid,"id,title as name,pid");
        return Show::json_show(Status::Success,"ok",$category);
    }


}